package com.report.component;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.report.dto.request.ScanCodePayReq;
import com.report.dto.request.ScanPayReq;
import com.report.dto.request.WechatReq;
import com.report.dto.response.MemberRes;
import com.report.dto.response.OrderRes;
import com.report.entity.Order;
import com.report.entity.Rule;
import com.report.properties.PayProperties;
import com.report.service.IMemberRuleService;
import com.report.service.IMemberService;
import com.report.service.IOrderService;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.exception.ResultException;
import com.zmh.framework.pay.model.PayOrderRequestDTO;
import com.zmh.framework.pay.service.impl.AlPay;
import com.zmh.framework.pay.service.impl.WxPay;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author summer
 * @date 2024-06-28 15:33
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ScanCodePayComponent {

    private final IMemberRuleService ruleService;

    private final IOrderService orderService;

    private final WxPay wxPay;

    private final PayProperties payProperties;

    private final AlPay alPay;


    /**
     * pc端扫码支付
     * @param scanCodePayReq
     */
    public ScanPayReq scanCodePay(ScanCodePayReq scanCodePayReq, HttpServletRequest request) {

        Rule memberRule = ruleService.getById(scanCodePayReq.getRuleId());
        MemberRes memberById = memberService.getMemberById(StpUtil.getLoginIdAsInt());
        if(memberById.getRuleId()==scanCodePayReq.getRuleId()){
            throw new ResultException("已购买当前会员,可以升级购买更高会员权益");
        }
        if(memberById.getRuleId()>scanCodePayReq.getRuleId()){
            throw new ResultException("可以升级购买更高会员权益");
        }



        String orderNo = IdUtil.simpleUUID();
        Order order=new Order();
        order.setMemberId(StpUtil.getLoginIdAsInt());
        order.setMemberRule(scanCodePayReq.getRuleId());
        order.setOrderNum(orderNo);
        order.setPayMethod(Constants.ZERO);
        order.setOrderPrice(memberRule.getPrice());
        order.setPayPlat(Constants.ONE);
        order.setProductName(memberRule.getName());
        orderService.save(order);
         //默认返回微信扫码支付码 memberRule.getPrice().toString()
        PayOrderRequestDTO payOrderRequestDTO=new PayOrderRequestDTO();
        payOrderRequestDTO.setOrderNo(orderNo);
        payOrderRequestDTO.setNotifyUrl(payProperties.getAliNotifyUrl());
        payOrderRequestDTO.setProductId(scanCodePayReq.getRuleId().toString());
        payOrderRequestDTO.setProductName(memberRule.getName());
        payOrderRequestDTO.setProductPrice(memberRule.getPrice().toString());
        String url = alPay.nativePay(payOrderRequestDTO, request);
        ScanPayReq scanPayReq=new ScanPayReq();
        scanPayReq.setOrderNo(orderNo);
        scanPayReq.setCodeUrl(url);
        return scanPayReq;
    }

    /**
     * 切换支付方式
     * @param scanPayReq
     * @return
     */
    public ScanPayReq changePayMethod(ScanPayReq scanPayReq,HttpServletRequest request) {
        LambdaQueryWrapper<Order> eq = Wrappers.lambdaQuery(Order.class).eq(Order::getOrderNum, scanPayReq.getOrderNo()).orderByDesc(Order::getCreateTime);
        Order order = orderService.getOne(eq);
        Rule rule = ruleService.getById(scanPayReq.getRuleId());
        order.setOrderPrice(rule.getPrice());
        order.setPayMethod(scanPayReq.getPaymentType());
        order.setMemberRule(scanPayReq.getRuleId());
        orderService.updateById(order);
        PayOrderRequestDTO payOrderRequestDTO=new PayOrderRequestDTO();
        payOrderRequestDTO.setOrderNo(order.getOrderNum());
        payOrderRequestDTO.setProductId(rule.getId().toString());
        payOrderRequestDTO.setProductName(rule.getName());
        payOrderRequestDTO.setNotifyUrl(payProperties.getWxNotifyUrl());
        payOrderRequestDTO.setProductPrice(rule.getPrice().toString());
        if(scanPayReq.getPaymentType()==Constants.ONE){
            payOrderRequestDTO.setNotifyUrl(payProperties.getWxNotifyUrl());
            scanPayReq.setCodeUrl(wxPay.nativePay(payOrderRequestDTO, request));
        }else{
            payOrderRequestDTO.setNotifyUrl(payProperties.getAliNotifyUrl());
            scanPayReq.setCodeUrl(alPay.nativePay(payOrderRequestDTO, request));
        }
        return scanPayReq;
    }


    private final IMemberService memberService;


    public WxPayMpOrderResult jsApiPay(WechatReq req,HttpServletRequest request) {
        MemberRes memberById = memberService.getMemberById(req.getMemberId());
        if(memberById.getRuleId()==req.getRuleId()){
            throw new ResultException("已购买当前会员,可以升级购买更高会员权益");
        }
        if(memberById.getRuleId()>req.getRuleId()){
            throw new ResultException("购买更高会员");
        }
        Rule memberRule = ruleService.getById(req.getRuleId());
        String orderNo = IdUtil.simpleUUID();
        Order order=new Order();
        order.setMemberId(req.getMemberId());
        order.setMemberRule(req.getRuleId());
        order.setOrderNum(orderNo);
        order.setPayMethod(Constants.ZERO);
        order.setOrderPrice(memberRule.getPrice());
        order.setPayPlat(Constants.ONE);
        order.setProductName(memberRule.getName());
        orderService.save(order);
        PayOrderRequestDTO payOrderRequestDTO=new PayOrderRequestDTO();
        payOrderRequestDTO.setOrderNo(orderNo);
        payOrderRequestDTO.setNotifyUrl(payProperties.getWxNotifyUrl());
        payOrderRequestDTO.setProductId(req.getRuleId().toString());
        payOrderRequestDTO.setProductName(memberRule.getName());
        payOrderRequestDTO.setProductPrice(memberRule.getPrice().toString());
        payOrderRequestDTO.setOpenId(req.getOpenId());
        payOrderRequestDTO.setAppId(req.getAppId());
        return wxPay.jsapiPay(payOrderRequestDTO,request);
    }
}
